package cn.edu.seu.ssny.controller.admin.system;

import cn.edu.seu.ssny.common.JsonResult;
import cn.edu.seu.ssny.controller.BaseController;
import cn.edu.seu.ssny.entity.Enterprise;
import cn.edu.seu.ssny.entity.Gateway;
import cn.edu.seu.ssny.service.IGatewayService;
import cn.edu.seu.ssny.service.specification.SimpleSpecificationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin/gateway")
public class GatewayController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(GatewayController.class);


    @Autowired
    private IGatewayService gatewayService;

    @RequestMapping(value = { "/", "/index" })
    public String index() {
        return "admin/gateway/index";
    }

    @RequestMapping(value = { "/list" })
    @ResponseBody
    public Page<Gateway> list() {
        SimpleSpecificationBuilder<Gateway> builder = new SimpleSpecificationBuilder<Gateway>();
        Page<Gateway> page = gatewayService.findAll(builder.generateSpecification(), getPageRequest());
        return page;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
        return "admin/gateway/form";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Integer id, ModelMap map) {
        Gateway gateway = gatewayService.find(id);
        map.put("gateway", gateway);
        return "admin/gateway/form";
    }

    @RequestMapping(value= {"/edit"} ,method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(Gateway gateway,ModelMap map, HttpSession session){
        try {
            gatewayService.saveOrUpdate(gateway, (Enterprise) session.getAttribute("enterprise"));
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@PathVariable Integer id, ModelMap map) {
        try {
            gatewayService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    @RequestMapping(value = "/deletems/{ids}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult deletems(@PathVariable String ids, ModelMap map) {
        if(ids == null)
            return JsonResult.failure("删除失败");
        String[] idArray = ids.split("&");
        for(String id: idArray) {
            try {
                gatewayService.delete(Integer.parseInt(id));
            } catch (Exception e) {
                e.printStackTrace();
                return JsonResult.failure(e.getMessage());
            }
        }
        return JsonResult.success();
    }

}
